package com.ygy.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.api.R;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ygy.entity.Products;
import com.ygy.entity.Users;
import com.ygy.mapper.ProductsMapper;
import com.ygy.service.IProductsService;
import com.ygy.service.impl.ProductsServiceImpl;
import com.ygy.utils.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 * 商品表 前端控制器
 * </p>
 *
 * @author Ye
 * @since 2024-03-14
 */
@CrossOrigin
@RestController
@RequestMapping("/pro")
public class ProductsController {
    @Autowired
    private ProductsServiceImpl productsService;

    @Autowired
    private ProductsMapper productsMapper;


    //增加商品
    @PostMapping("/add")
    public Result addProduct(@RequestBody Products product) {

        productsService.save(product);
        return Result.success(product,"商品添加成功"); // 保存后返回原对象，回数据库中的完整对象
    }

    //获取商品列表
    @GetMapping("/list")
    public Result<List<Products>> getAllProducts() {
        List<Products> products = productsService.list();
        return  Result.success(products) ;
    }

    ////批量删除
    @PostMapping("/delete")
    public Result deleteProduct( @RequestBody List<Integer> productId) {
        System.out.println("进入删除");
        System.out.println(productId+"++++++++++++++++++++============+++++++");
        for (Integer productIds : productId) {
            boolean delete = productsService.removeById(productIds);
            if (!delete) {
                return Result.error("删除失败");
            }
        }
        return Result.success();
    }

    //修改商品信息
    @PutMapping("/update")
    public Result updateProduct(@RequestBody Products product) {
        System.out.println("==================="+product.getProductName()+"===============");
        boolean update = productsService.saveOrUpdate(product);
        //  判断
        if (update) {
            return Result.success(product,product.getProductName()+"用户信息更新成功");
        } else {
            return Result.error("失败");
        }
    }

    //分页查询和模糊查询
    @GetMapping("/page")
    public IPage<Products> findPage(@RequestParam Integer pageNum,
                                    @RequestParam Integer pageSize,
                                    @RequestParam(defaultValue = "") String productName,
                                    @RequestParam(required = false) Integer category){
        System.out.println("=======进来分页接口========"+productName);
        IPage<Products> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Products> queryWrapper = new QueryWrapper<>();
        // 模糊查询产品名称
        if (!productName.isEmpty()) {
            queryWrapper.like("product_name", productName);
        }
        // 根据分类过滤
        if (category != null) {
            queryWrapper.eq("category", category);
        }
       // queryWrapper.like("product_name", productName);
        return productsService.page(page, queryWrapper);
    }

    //获取个人商品列表
    /*@GetMapping("/createUser")
    public Result<List<Products>> getAllProducts(@RequestParam Integer createUser) {

        // 使用QueryWrapper来构建查询条件
        QueryWrapper<Products> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("createuser", createUser);
        // 执行查询并获取结果
        List<Products> productsList = productsMapper.selectList(queryWrapper);

        // 返回查询结果
        return  Result.success(productsList) ;
    }*/


    // 分页精确查询（按 createUserId）
    @GetMapping("/wofabu")
    public IPage<Products> findPageByCreateUserId(@RequestParam Integer pageNum,
                                                  @RequestParam Integer pageSize,
                                                  @RequestParam Integer createuser) {
        System.out.println("=======进来分页接口========" + createuser);
        IPage<Products> page = new Page<>(pageNum, pageSize);
        QueryWrapper<Products> queryWrapper = new QueryWrapper<>();
        // 使用 eq 方法进行精确查询
        queryWrapper.eq("createuser", createuser);
        // 执行分页查询
        return productsService.page(page, queryWrapper);
    }

    //单个商品信息
    @GetMapping("/productId")
    public Result getProducts(@RequestParam(required = false) Integer productId) {
    Products  products = productsService.getById(productId);
        return  Result.success(products) ;
    }

    //改变商品状态
    @PutMapping("/{productId}/status")
    public Result updateProductStatus(@PathVariable Integer productId,@RequestParam Integer newStatus){


         if (productId == null || newStatus == null){
             //商品不存在
             return Result.error("修改商品状态失败");
         }
         //更新状态
        Products product = productsService.getById(productId);

        product.setStatus(newStatus);
         boolean update = productsService.updateById(product);
        if (update) {
            // 更新成功
            return Result.success("商品状态更新成功");
        } else {
            // 更新失败
            return Result.error("商品状态更新失败");
        }
    }
    // 查询所有在售商品接口
    @GetMapping("/onsale")
    public Result<List<Products>> findAllOnSale() {
         List<Products> allOnSale = productsService.findAllOnSale();
        return Result.success(allOnSale) ;
    }

    }
